package com.travelexp;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapViewer extends MapActivity {
	private TravelData travelData;
	private MapItemizedOverlay itemizedoverlay;
	public static final String UPDATE_MAPS = "PatetaPls";
	MapView mapView;
	List<Overlay> mapOverlays;
	String currentTravel;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    travelData = new TravelData(this);
	    
	    // We will have to set all the pins again if the travel change
	    currentTravel = travelData.getCurTravel();
	    
	    setContentView(R.layout.map_viewer);
	    // Zoom controls (yes, it's easy to handle in the emulator)
	    mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    		
	    // Overlay used for the pin points	    
	    mapOverlays = mapView.getOverlays();
	    
	    // Set the pinpoints
	    setPinpoints();
	}

	public void setPinpoints()
	{
	    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_action_pinpoint);
	    itemizedoverlay = new MapItemizedOverlay(drawable, this);

		// No travel is set, return then
		if(currentTravel == null) return;
		String selection = TravelData.C_TRAVEL + "=?";
		Cursor c = travelData.execQuery(TravelData.T_LOCAL, null, selection, new String[] {currentTravel}, null);

        int longInd = c.getColumnIndex(TravelData.C_LONGITUDE);
        int latInd = c.getColumnIndex(TravelData.C_LATITUDE);
        int timeInd = c.getColumnIndex(TravelData.C_TIME);
        c.moveToFirst();
        while (c.moveToNext()) {
        	int lat = (int) (c.getFloat(latInd) * 1E6);
        	int lon = (int) (c.getFloat(longInd) * 1E6);
        	String time = c.getString(timeInd);
        	GeoPoint point = new GeoPoint(lat, lon);
        	OverlayItem overlayItem = new OverlayItem(point, "Pos", time);
        	itemizedoverlay.addOverlay(overlayItem);
        }
        c.close();		
        mapOverlays.add(itemizedoverlay);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void updateMap()
	{
		// If the current travel has changed
		String currentTravel = travelData.getCurTravel();
		if(currentTravel != null)
		{
			// Remove the current overlays
			mapOverlays.remove(itemizedoverlay);
			setPinpoints();
		}
	}
	
	// Our handler for received Intents. This will be called whenever an Intent
	// with an action named "custom-event-name" is broadcasted.
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
	  @Override
	  public void onReceive(Context context, Intent intent) {
		  updateMap();
	  }
	};		

}
